<script setup lang="ts">
import {
  Menu, Grid, PieChart, ArrowDown, HomeFilled, ChatDotRound
  , Calendar,} from "@element-plus/icons-vue";
import { ref } from 'vue';
import { useRouter } from 'vue-router'

const router = useRouter()
const userMenuVisible = ref(false);
const activeMenu = ref('/mainframe/homepage');

const toggleUserMenu = () => {
  userMenuVisible.value = !userMenuVisible.value;
};

const closeUserMenu = () => {
  userMenuVisible.value = false;
};

// 点击页面其他位置关闭下拉菜单
document.addEventListener('click', (e) => {
  if (!e.target.closest('.user-info')) {
    closeUserMenu();
  }
});

const Esc = () => {
  router.push('/');
  alert("感谢您的使用")
}

const users = () => {
  router.push('/userlist');
  closeUserMenu();
}
</script>

<template>
  <div class="app-container">
    <!-- 头部开始 -->
    <div class="header-container">
      <div class="system-title">
        <el-icon class="title-icon"><Activity /></el-icon>
        糖尿病智能健康助理帅
      </div>
      <div class="header-spacer"></div>
      <div class="header-stats">
        <div class="stat-item">
          <span class="stat-label">今日监测</span>
          <span class="stat-value">0</span>
        </div>
        <div class="stat-item">
          <span class="stat-label">异常提醒</span>
          <span class="stat-value" :class="{ warning: true }">0</span>
        </div>
      </div>
      <div class="user-info" @click.stop="toggleUserMenu">
        <el-avatar :size="30" src="https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png" />
        <span class="user-name">管理员</span>
        <el-icon class="arrow-icon"><ArrowDown /></el-icon>

        <!-- 用户下拉菜单 -->
        <transition name="el-zoom-in-top">
          <div v-show="userMenuVisible" class="user-dropdown">
            <div class="dropdown-item" @click="users">个人中心</div>
            <el-divider />
            <div class="dropdown-item" @click.stop="Esc">退出登录</div>
          </div>
        </transition>
      </div>
    </div>

    <!-- 主体内容区域 -->
    <div class="main-content">
      <!-- 左侧导航菜单 -->
      <div class="sidebar">
        <div class="sidebar-header">功能导航</div>
        <el-menu
            router
            :default-active="activeMenu"
            @select="(index) => activeMenu = index"
            style="border:0; flex: 1;"
            background-color="#f9fbff"
            text-color="#333"
            active-text-color="#30B0C7"
            active-background-color="#e6f7f0"
        >
          <el-menu-item index="/mainframe/homepage">
            <el-icon><HomeFilled /></el-icon>
            <span>首页</span>
          </el-menu-item>
          <el-menu-item index="/mainframe/patientinformation">
            <el-icon><Activity /></el-icon>
            <span>病人管理</span>
          </el-menu-item>
          <el-menu-item index="/mainframe/DiabetesPrediction">
            <el-icon><PieChart /></el-icon>
            <span>数据预测</span>
          </el-menu-item>
          <el-menu-item index="/mainframe/KnowledgeBase">
            <el-icon><Book /></el-icon>
            <span>糖尿病知识库</span>
          </el-menu-item>
          <el-menu-item index="/mainframe/qa">
            <el-icon><ChatDotRound /></el-icon>
            <span>智能问答</span>
          </el-menu-item>

          <el-sub-menu index="3">
            <template #title>
              <el-icon><MedicineBox /></el-icon>
              <span>药品管理</span>
            </template>
            <el-menu-item index="/mainframe/medicine-purchase">药品进货管理</el-menu-item>
            <el-menu-item index="/mainframe/medicine-stock">库存查询与预警</el-menu-item>
          </el-sub-menu>

          <el-menu-item index="/mainframe/reminder">
            <el-icon><AlertCircle /></el-icon>
            <span>健康提醒</span>
          </el-menu-item>
        </el-menu>
      </div>

      <!-- 右侧主题区域 -->
      <div class="content-area">
        <router-view/>
      </div>
    </div>
  </div>
</template>

<style scoped>
/* 全局容器设置 */
.app-container {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

/* 头部样式优化 */
.header-container {
  height: 60px;
  background-color: #30B0C7; /* 医疗健康类应用更适合的青绿色 */
  display: flex;
  align-items: center;
  padding: 0 20px;
  position: relative;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  z-index: 10;
}

.system-title {
  display: flex;
  align-items: center;
  font-size: 18px;
  color: white;
  font-weight: 600;
  padding: 0 15px;
}

.title-icon {
  margin-right: 8px;
  font-size: 22px;
}

.header-spacer {
  flex: 1;
}

/* 头部统计信息 */
.header-stats {
  display: flex;
  margin-right: 20px;
}

.stat-item {
  margin: 0 15px;
  text-align: center;
}

.stat-label {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.8);
  display: block;
}

.stat-value {
  font-size: 16px;
  color: white;
  font-weight: 600;
}

.stat-value.warning {
  color: #FFE066;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% { opacity: 1; }
  50% { opacity: 0.6; }
  100% { opacity: 1; }
}

/* 用户信息区域 */
.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
  position: relative;
  padding: 5px 10px;
  border-radius: 4px;
  transition: background-color 0.2s;
}

.user-info:hover {
  background-color: rgba(255, 255, 255, 0.2);
}

.user-name {
  color: white;
  margin-left: 8px;
  margin-right: 4px;
  font-size: 14px;
}

.arrow-icon {
  color: white;
  font-size: 12px;
  transition: transform 0.3s;
  transform: rotate(0deg);
}

.user-info:hover .arrow-icon {
  transform: rotate(180deg);
}

/* 用户下拉菜单 */
.user-dropdown {
  position: absolute;
  top: 100%;
  right: 0;
  background-color: white;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  z-index: 1000;
  min-width: 120px;
  margin-top: 5px;
  border: 1px solid #e5e6eb;
}

.dropdown-item {
  padding: 8px 16px;
  font-size: 14px;
  color: #606266;
  cursor: pointer;
  transition: all 0.2s;
}

.dropdown-item:hover {
  background-color: #f0f7f4;
  color: #30B0C7;
  padding-left: 20px;
}

.el-divider {
  margin: 4px 0;
}

/* 主体内容布局优化 */
.main-content {
  display: flex;
  height: calc(100vh - 60px);
  width: 100%;
}

.sidebar {
  width: 230px;
  border-right: 1px solid #e5e6eb;
  background-color: #f9fbff;
  display: flex;
  flex-direction: column;
  box-shadow: 1px 0 3px rgba(0,0,0,0.05);
}

.sidebar-header {
  padding: 15px 20px;
  font-size: 12px;
  color: #888;
  border-bottom: 1px solid #e5e6eb;
  text-transform: uppercase;
  letter-spacing: 1px;
}

/* 菜单样式优化 */
.el-menu {
  border-right: none;
  padding-top: 10px;
}

.el-menu-item, .el-sub-menu__title {
  height: 46px;
  line-height: 46px;
  padding-left: 25px !important;
  transition: all 0.3s;
}

.el-menu-item:hover, .el-sub-menu__title:hover {
  background-color: #f0f7f4 !important;
}

.el-menu-item.is-active {
  border-left: 3px solid #30B0C7;
}

.el-menu-item .el-icon, .el-sub-menu__title .el-icon {
  margin-right: 12px;
  font-size: 18px;
}

/* 内容区域 */
.content-area {
  flex: 1;
  background-color: #f5f7fa;
  padding: 20px;
  box-sizing: border-box;
  overflow-y: auto;
}

/* 响应式调整 */
@media (max-width: 992px) {
  .sidebar {
    width: 60px;
  }

  .sidebar-header, .el-menu-item span, .el-sub-menu__title span {
    display: none;
  }

  .el-menu-item, .el-sub-menu__title {
    padding-left: 15px !important;
    justify-content: center;
  }

  .el-menu-item .el-icon, .el-sub-menu__title .el-icon {
    margin-right: 0;
  }

  .system-title span {
    display: none;
  }

  .system-title {
    justify-content: center;
  }

  .header-stats {
    display: none;
  }
}
</style>